package communicationMessages;

import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;

import logger.LoggerWriter;
import data.ForumMessage;
import database.DB;

public class AddThreadMessage extends Message {

	@Override
	public String getAnswer() {
		LoggerWriter logger = LoggerWriter.getLoggerWriter();

		String copyStr=this.content;
		String [] arr =new String [4];
		int endIndex,beginIndex;
		beginIndex=0;
		for(int i=0; i<4;i++){
			endIndex=copyStr.indexOf(delimiter);
			arr[i]=copyStr.substring(beginIndex, endIndex);
			copyStr=copyStr.substring(endIndex+1);

		}
		String userName,subject,body,subForumId;
		userName = arr[0];
		subForumId = arr[1];
		subject = arr[2];
		body = arr[3];
		String ans = "";
		if (!ForumMessage.isLegalMsg(subject, body))
		{
			logger.log("failed"+delimiter+"add thread Msg wrong format", Level.WARNING);
			ans = "failed"+delimiter+"add thread Msg wrong format"+delimiter;
		}
		else
		{

			int userId= DB.getUserId(userName);
			if (!DB.isUserExist(userId))
			{
				ans = "user dosen't exist in the system";
				return ans;
			}
			boolean isSubForumExist = DB.isSubForumExist(subForumId);
			if (!isSubForumExist){
				logger.log("subForum is null the number is " + subForumId , Level.WARNING);
				ans = "pathNotFound" + delimiter;
				return ans;
			}
			if (!DB.isUserBannedFromSubForum(userId, subForumId))
			{
				addToBannedUsersIfNeeded(userName, userId, subForumId);
				int threadId = DB.addThreadMsg(subForumId,userId,subject,body,new Date(System.currentTimeMillis()));
				System.out.println("the thread id is " + threadId);
				String msg = "popupMessage"+delimiter+userName+delimiter+subject+delimiter+body+delimiter;
				String webPopupMsg = getAddThreadPopupMsg(threadId); 
				this.sendMsgToObserver(subForumId, msg,webPopupMsg);
				ans ="addThread"+delimiter+"success"+delimiter;
				try {
					sendRefreshMsg(logger, subForumId,threadId,false);
				} catch (SQLException e) {
					System.out.println("SQLException in AddThreadMessage" + e.getMessage());
					
				}
			}
			else
			{
				ans ="addThread"+delimiter+"Failed\nyou are banned from this subforum"+delimiter;
			}
		}



		return ans;
	}
	/*else
		{
			String userName,subject,body,subForumId;
			userName = arr[0];
			subForumId = arr[1];
			subject = arr[2];
			body = arr[3];
			MemberDataBase tUserDataBase =MemberDataBase.getMembersDataBase();
			Member m = tUserDataBase.getMember(userName);
			if (m == null)
			{
				ans = "user dosen't exist in the system";
				return ans;

			}
			Forum forum = Forum.getForum();
			SubForum subForum = forum.getSubForum(Integer.valueOf(subForumId));
			if (subForum == null){
				logger.log("subForum is null the number is " + subForumId , Level.WARNING);
				ans = "subForum is null the number is " + subForumId;
				return ans;
			}


			subForum.addThreadMsg(subject, body,m, new Date(System.currentTimeMillis()));
			Observer.addNewMsg("popupMessage"+delimiter+userName+delimiter+subject+delimiter+body+delimiter);
			ans ="addThread"+delimiter+"success"+delimiter;
		}*/





}
